package cn.com.centaline.test2

import android.app.Activity
import android.widget.Toast
import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

/** Test2Plugin */
class Test2Plugin : FlutterPlugin, MethodCallHandler, ActivityAware, EventChannel.StreamHandler {
    private lateinit var methodChannel: MethodChannel
    private lateinit var eventChannel: EventChannel
    private lateinit var activity: Activity
    private var eventSink: EventChannel.EventSink? = null


    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        println("onAttachedToEngine invoke")
        methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "test2")
        methodChannel.setMethodCallHandler(this)
        eventChannel = EventChannel(flutterPluginBinding.binaryMessenger, "test2flutter")
        eventChannel.setStreamHandler(this)

    }

    companion object {
        @JvmStatic
        fun registerWith(registrar: Registrar) {
            println("registerWith invoke")
            val test2Plugin = Test2Plugin()
            val methodChannel = MethodChannel(registrar.messenger(), Constants.NATIVE_VOICE_CHANNEL)
            methodChannel.setMethodCallHandler(test2Plugin)
            val eventChannel = EventChannel(registrar.messenger(), Constants.NATIVE_VOICE_CHANNEL_BACK)
            eventChannel.setStreamHandler(test2Plugin)
        }
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        if (call.method == "getPlatformVersion") {
            Toast.makeText(activity, "Android ${android.os.Build.VERSION.RELEASE}", Toast.LENGTH_LONG).show()
            result.success("Android ${android.os.Build.VERSION.RELEASE}")

            // 原生调回flutter
            eventSink?.success("android native 回调原生")

        } else {
            result.notImplemented()
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        methodChannel.setMethodCallHandler(null)
        eventChannel.setStreamHandler(null)
    }

    override fun onDetachedFromActivity() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        activity = binding.activity
    }

    override fun onDetachedFromActivityForConfigChanges() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
        eventSink = events
    }

    override fun onCancel(arguments: Any?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}
